package adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hjh.qinxin.R;

import java.util.List;

import model.PopularizeListModel;
import model.ReportListModel;

public class ReportListAdapter extends RecyclerView.Adapter<ReportListAdapter.ReportListHolder>{
    private Context context;
    public List<ReportListModel> data;
    private OnRecyclerViewItemClickListener listener;

    public void setData(List<ReportListModel> data){
        this.data = data;
    }
    public ReportListAdapter(Context context){
        this.context = context;
    }
    @Override
    public ReportListHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        return new ReportListHolder(LayoutInflater.from(context). inflate(R.layout.report_list_item,null));
    }

    @Override
    public void onBindViewHolder(@NonNull  ReportListAdapter.ReportListHolder holder, int position) {

        if (!data.get(position).getUrl().isEmpty()){
            Glide.with(context)
                    .load(data.get(position)
                    .getUrl())
                    .into(holder.relistPicture);
        }else {
            holder.relistPicture.setImageResource(R.drawable.ic_launcher_foreground);
        }
        holder.relistHeadline.setText(data.get(position).getListHeadline());
        holder.relistTime.setText(data.get(position).getListTime());

        holder.relistPicture.setOnClickListener(new View.OnClickListener() {//条目点击
            @Override
            public void onClick(View view) {
                if(null != listener){
                    listener.onItemClick(position);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return null != data ? data.size() : 0;
    }

    class ReportListHolder extends RecyclerView.ViewHolder{
        ImageView relistPicture;
        TextView relistHeadline;
        TextView relistTime;


        public ReportListHolder(@NonNull View itemView) {
            super(itemView);
            relistPicture = itemView.findViewById(R.id.Homeft_report_list_picture);
            relistHeadline = itemView.findViewById(R.id.Homeft_report_list_headline);
            relistTime = itemView.findViewById(R.id.Homeft_report_list_time);

        }
    }
    public interface OnRecyclerViewItemClickListener{


        void onItemClick(int position);
    }
}
